package org.FaPe3D.utils
{

	import org.FaPe3D.Adapter;
	import org.FaPe3D.core.events.EventManager;
	import org.FaPe3D.core.parsers.casting.Events;
	import org.papervision3d.events.FileLoadEvent;
	
	public class RegisterEvents extends EventManager
	{
		private static function registerEvent(element:*, key:String, params:Array, func:Function):void
		{
			 element.addEventListener(key, function(e:*)
				{
					params[0] = e;
					func.apply(null, params);
				}); 
		}
		
		public static function register(id:String, element:*, events:Object = null):void
		{
			var _events:Events = new Events();
			var key:String;
			var func:Function;
			var params:Array;
			var commonClass:* = Adapter.getAdapter(id).commonClass
			if(element != null && events != null)
			{
				for(var item in events)
				{
					try
					{
						params = new Array(null, id);
						key = _events.cast(events[item].type)
						func = commonClass[events[item]['function'].name];
						params = params.concat(events[item]['function'].arguments)
						registerEvent(element, key, params, func);
					}catch(e:*)
					{
						trace('Error:', e);
					}
				}
			}
		}
		
	}
}